package com.fulei.logic;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

public class FSKCSMOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 109;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int beginCSM()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 102, 3, 0, 0, 0, 105 };
      
      LogUtil.writeCSMLog("beginCSMSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      Thread.sleep(200L);
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 6);
      if (ReadDataLength < 4) {
        return -1;
      }
      LogUtil.writeCSMLog("beginCSMRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if (ReadData[0] == 102) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("压缩机开启异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int closeCSM()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 102, 3, 0, 1, 0, 106 };
      
      LogUtil.writeCSMLog("closeCSMSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 6);
      if (ReadDataLength < 4) {
        return -1;
      }
      LogUtil.writeCSMLog("closeCSMRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if (ReadData[0] == 102) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("压缩机关闭异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int allCloseCSM()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 102, 3, 0, 7, 0, 112 };
      
      LogUtil.writeCSMLog("allCloseCSMSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 6);
      if (ReadDataLength < 4) {
        return -1;
      }
      LogUtil.writeCSMLog("allCloseCSMRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if (ReadData[0] == 102) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("压缩机关闭异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int tempCheckCSM()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 102, 1, 0, 103 };
      
      LogUtil.writeCSMLog("tempCheckCSMSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      Thread.sleep(200L);
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 14);
      if (ReadDataLength < 12) {
        return -1;
      }
      LogUtil.writeCSMLog("tempCheckCSMRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if (ReadData[0] == 102)
      {
        return_data = ReadData[10];
        return_data = temperMapVal[(166 - return_data)];
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("压缩机温度检测异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  static char[] temperMapVal = { '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '\000', '\000', '\000', '\000', '\000', '\000', '\001', '\001', '\001', '\002', '\002', '\002', '\003', '\003', '\003', '\004', '\004', '\004', '\004', '\005', '\005', '\005', '\006', '\006', '\006', '\007', '\007', '\007', '\b', '\b', '\b', '\t', '\t', '\b', '\n', '\n', '\n', '\013', '\013', '\013', '\f', '\f', '\f', '\r', '\r', '\r', '\016', '\016', '\017', '\017', '\017', '\020', '\020', '\020', '\021', '\021', '\022', '\022', '\022', '\023', '\023', '\024', '\024', '\024', '\025', '\025', '\026', '\026', '\027', '\027', '\030', '\030', '\031', '\031', '\031', '\032', '\032', '\033', '\033', '\034', '\035', '\035', '\036', '\036', '\037', '\037', ' ', ' ', '!', '"', '"', '#', '$', '$', '%', '&', '&', '\'', '(', ')', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '8', '9', ':', '<' };
  
  public static byte SumByte(byte[] data, int offset, int length)
  {
    int Result = 0;
    for (int i = offset; i <= offset + length; i++) {
      Result += data[i];
    }
    return (byte)Result;
  }
}
